Skip to content

fix: MoLang optimization quick-fix range resolution to prevent malformed rewrites#497

Merged
DaanV2 merged 2 commits into
mainfrom
copilot/fix-rewrite-constant-functions
May 15, 2026
Merged

fix: MoLang optimization quick-fix range resolution to prevent malformed rewrites#497
DaanV2 merged 2 commits into
mainfrom
copilot/fix-rewrite-constant-functions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

MoLang optimization diagnostics were generating correct replacement text (math.pi no longer math.pi()), but quick-fix application still used an incorrect edit range in some cases. This caused partial-token replacement and corrupted output such as 5*3(math.pi() * 15).pi.

  • Root cause

    • Optimization code actions relied on diag.range, which could point to an inner/misaligned span instead of the full expression intended for rewrite.
  • Diagnostic payload now carries source offsets

    • packages/bedrock-diagnoser/src/diagnostics/molang/optimizations/registry.ts
    • Added startOffset and endOffset to optimization diagnostic data when a replacement is present.
    • Keeps existing replacement behavior while giving code actions an unambiguous source span.
  • Code action now prefers offset-derived range

    • ide/base/server/src/lsp/code-action/minecraft/molang/optimization.ts
    • Quick fix computes editRange from startOffset/endOffset when available; falls back to diag.range for compatibility.
    • Ensures rewrite applies to the full expression (5*3*math.pi) rather than a partial segment.
  • Regression coverage

    • Added server-side code-action regression test:
      • ide/base/server/src/lsp/code-action/minecraft/molang/optimization.test.ts
      • Verifies malformed diag.range does not affect replacement when offsets are present.
    • Updated optimization tests to allow expanded diagnostic data shape and assert offset fields for the 5*3*math.pi regression path:
      • packages/bedrock-diagnoser/test/lib/diagnostics/molang/optimizations.test.ts
const hasOffsetRange =
  typeof startOffset === 'number' &&
  typeof endOffset === 'number' &&
  endOffset >= startOffset;

const editRange = hasOffsetRange
  ? {
      start: builder.context.document.positionAt(startOffset),
      end: builder.context.document.positionAt(endOffset),
    }
  : diag.range;

Copilot AI changed the title [WIP] Fix molang rewrite for math constants Fix MoLang optimization quick-fix range resolution to prevent malformed rewrites May 15, 2026
Copilot finished work on behalf of DaanV2 May 15, 2026 15:39
Copilot AI requested a review from DaanV2 May 15, 2026 15:39
@DaanV2 DaanV2 marked this pull request as ready for review May 15, 2026 20:04
@DaanV2 DaanV2 changed the title Fix MoLang optimization quick-fix range resolution to prevent malformed rewrites fix: MoLang optimization quick-fix range resolution to prevent malformed rewrites May 15, 2026
@DaanV2 DaanV2 merged commit 0e75182 into main May 15, 2026
4 checks passed
@DaanV2 DaanV2 deleted the copilot/fix-rewrite-constant-functions branch May 15, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Molang rewrite attempts to turn math constants into functions

2 participants